import sys

sys.path.append("..")
from common.function.common_func import typeof
from common.function.common_func import isset

def list_to_dict(list, key=''):
    """
        只支持此格式 [{},{}]
        此函数支持将列表转化为字典
        转化后的格式为{{},{}}
        :param list: 列表
        :param key:  转化后字典的键值 默认键值就为0开始的键值
        :return:
    """

    if typeof(list) == 'list':
        new_dict = {}
        index = 0
        for val in list:
            if typeof(val) != 'dict' and typeof(val) != 'set':
                return list
            if key != '' and key in val:
                new_dict[val[key]] = val
            else:
                new_dict[index] = val
            index += 1
        return new_dict
    else:
        return list

def get_list_dict_column(list_data, column):
    """ 获取列表中二级字典的某一个指定键值 返回数组
        :param list_data:
        :param column:
        :return:
    """
    if not list_data:
        return []

    new_list = []
    for list_val in list_data:
        if typeof(list_val) != "dict":
            return []
        elif isset(list_val, column):
            new_list.append(list_val[column])
    return new_list